﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using TextGlow.Control.Utilities;
using System.Linq;

namespace TextGlow.Control.Controls.Document
{
  public class ParagraphLine : WrapPanel
  {
    private bool _whiteSpaceEnd;
    private Unit _spacingLine;
    private double? _lineSpacingPixels;

    public Unit SpacingLine
    {
      get { return _spacingLine; }
      set { _spacingLine = value; }
    }

    public bool WhiteSpaceEnd
    {
      get { return _whiteSpaceEnd; }
      set { _whiteSpaceEnd = value; }
    }

    public ParagraphLine()
    {
      Orientation = Orientation.Horizontal;
    }

    public override string ToString()
    {
      StringBuilder contents = new StringBuilder();

      foreach (FrameworkElement element in Children)
      {
        if (element is TextBlock)
          contents.Append(((TextBlock)element).Text);
        else if (element is Image)
          contents.Append("{Image}");
        else
          contents.Append(element.ToString());
      }

      return contents.ToString();
    }

    public double? LargestFontSize
    {
      get
      {
        return Children
          .Select(v => v as TextBlock)
          .Where(t => t != null)
          .Max(t => (double?)t.FontSize);
      }
    }

    protected override Size CalculateElementSize(UIElement element)
    {
      Size elementSize;

      TextBlock t = element as TextBlock;
      FrameworkElement e = element as FrameworkElement;
      bool sizeHack = false;
      if (t != null)
      {
        sizeHack = (t.Text.Length == 0);
        if (sizeHack)
          t.Text = " ";
        elementSize = new Size(t.ActualWidth, t.ActualHeight);
        if (sizeHack)
          t.Text = "";
      }
      else if (e != null)
      {
        elementSize = new Size(e.Width, e.Height);
      }
      else
      {
        throw new Exception("Could not get element size.");
      }
      //Size elementSize = (t != null) ? new Size(t.ActualWidth, t.ActualHeight) : ((FrameworkElement)element).w.DesiredSize;

      double width = HasItemWidth ? ItemWidth : elementSize.Width;
      double height = HasItemHeight ? ItemHeight : elementSize.Height;

      return new Size(width, height);
    }

    protected override Size CustomMeasureOverride(Size constraint)
    {
      Size initialSize = base.CustomMeasureOverride(constraint);

      if (_spacingLine.Type == UnitType.Percent && _spacingLine.Value > 1 && LargestFontSize != null)
      {
        _lineSpacingPixels = LargestFontSize.Value * (_spacingLine.Value - 1);
        return new Size(initialSize.Width, initialSize.Height + _lineSpacingPixels.Value);
      }
      else
      {
        _lineSpacingPixels = null;
        return initialSize;
      }
    }

    protected override Size CustomArrangeOverride(Size finalSize)
    {
      foreach (UIElement childElement in Children)
      {
        FrameworkElement frameworkElement = childElement as FrameworkElement;
        if (frameworkElement != null)
          frameworkElement.VerticalAlignment = VerticalAlignment.Bottom;
      }

      return base.CustomArrangeOverride(new Size(finalSize.Width, finalSize.Height - _lineSpacingPixels.GetValueOrDefault()));
    }

    //protected override void PerformLayout()
    //{
    //  base.PerformLayout();

    //  if (_spacingLine.Type == UnitType.Percent && _spacingLine.Value > 1 && LargestFontSize != null)
    //  {
    //    double spacingPixels = LargestFontSize.Value * (_spacingLine.Value - 1);

    //    ContentBounds = LayoutUtils.CreateRect(ContentBounds.GetLocation(), new Size(ContentBounds.Width, ContentBounds.Height + spacingPixels));
    //  }
    //  //
    //}
  }
}
